package eraserhead.controller;

import eraserhead.boundary.CreateEventForm;
import eraserhead.boundary.LoginForm;
import eraserhead.boundary.WelcomeForm;
import eraserhead.entity.User;
import eraserhead.xml.request.SignInRequest;

/**
 * Controller of the login panel
 * @author Quanquan
 */
public class LoginController {
	//Decision model;
	LoginForm view;
	
	/**
	 * Creates a new {@link LoginController}
	 * @param view {@link LoginForm} to control
	 */
	public LoginController(LoginForm view) {
		//this.model = model;
		this.view = view;
	}
	
	public void process() {
		if (view.getIsModerator()) {
			User moderator = new User(view.getName(), view.getPassword(), true);
			CreateEventForm cef = new CreateEventForm(moderator);
			view.dispose();
			cef.setVisible(true);
			
			//model = view.getModel();
			//TODO: sign in request
			//SignInRequest.send(ServerAccessManager.getAccess(), view.getEventID(), view.getName(), view.getPassword());
			
		} else {
			String eventID = view.getEventID();
			User.storeUser(new User(view.getName(), view.getPassword(), false));
			SignInRequest.send(ServerAccessManager.getAccess(), eventID, view.getName(), view.getPassword());
			System.out.println("Sign in request should be sent");
			view.dispose();
			//TODO: get model according to the event id from the server
			//TODO: eventID not found -> warning
			//TODO: event full -> warning
			//need to initiate model base on the server's response, should be done in the SignInResponseController
		}
		
		
		
		// handled by the server?
		//if (!model.userJoin(new User(view.getName(), view.getPassword(), view.getIsModerator()))) {
		//	WarningWindow.giveWarning(view, "Invalid name or password");
		//}
		//DecisionGUI dg = new DecisionGUI(model);
		//view.dispose();
		//dg.setVisible(true);
	}
	
	/**
	 * Cancel the login and show the {@link WelcomeForm}
	 */
	public void cancel() {
        view.dispose();
        WelcomeForm wf = new WelcomeForm();
		wf.setVisible(true);
	}
}
